/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.startup;

import ca.trader.logger.TraderLogger;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;
import org.eclipse.persistence.sessions.UnitOfWork;

/**
 *
 * @author Ahsan
 */
public class SetDatabaseTableMyISAM implements SessionCustomizer
{

	 private static final String _className = SessionCustomizer.class.getName();
	 
	 @Override
	 public void customize(Session session) throws Exception
	 {
		  session.getEventManager().addListener(new SessionEventAdapter()
		  {
			   @Override
			   public void postAcquireClientSession (SessionEvent event)
			   {
				    TraderLogger.logInfo(_className, "customize", "Starting executing post deployment database script.");
				    
				    UnitOfWork unitOfWork = event.getSession().acquireUnitOfWork();
				    
				    unitOfWork.executeNonSelectingSQL("ALTER TABLE `sale` ENGINE=MYISAM");
				    unitOfWork.executeNonSelectingSQL("ALTER TABLE `user` ENGINE=MYISAM");
				    unitOfWork.executeNonSelectingSQL("ALTER TABLE `useractivationdesc` ENGINE=MYISAM");
				    unitOfWork.executeNonSelectingSQL("ALTER TABLE `userloginhistoryrecord` ENGINE=MYISAM");
				    unitOfWork.executeNonSelectingSQL("ALTER TABLE `user_sale` ENGINE=MYISAM");
				    unitOfWork.executeNonSelectingSQL("ALTER TABLE `sale` ADD FULLTEXT (TITLE, DESCRIPTION)");
				    

				    TraderLogger.logInfo(_className, "customize", "Executed post deployment database script.");
				    
				    unitOfWork.commit();
			   }

		  });
	 }

}
